﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 1, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };

    //全选
    form.on('checkbox(allChoose)', function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断文章是否全部选中来确定全选按钮是否选中
    form.on("checkbox(choose)", function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
        if (childChecked.length == child.length) {
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
        } else {
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
        }
        form.render('checkbox');
    });

    $("body").on("click", ".news_del", function () {  //删除
        var _this = $(this);
        layer.confirm('确定删除此信息？', { icon: 3, title: '提示信息' }, function (index) {
            var index = layer.msg('删除中，请稍候', { icon: 16, time: false, shade: 0.8 });
            $.ajax({
                type: "post",
                url: "/Personal/Delete",
                data: JSON.stringify({ Id: _this.attr("data-id") }),
                success: function (result) {
                    layer.msg(result.Message);
                    getList(result.Data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(textStatus);
                }
            });
            layer.close(index);

        });
    });

    //表格数据和分页
    function getList(that) {
        $.ajax({
            type: "POST",
            url: "/Employment/List",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;
                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //执行加载数据的方法
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });
        //渲染数据
        function renderDate(list) {
            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {
                    var plantype="";
                    switch (item.PlanType) {
                        case 0:
                            plantype = "月计划";
                            break;
                        case 1:
                            plantype = "季度计划";
                            break;
                        case 2:
                            plantype = "年度计划";
                            break;
                    }
                    dataHtml += '<tr>'
                    + '<td>' + item.Id + '</td>'
                    + '<td>' + item.DepartmentName + '</td>'
                    + '<td>' + item.PositionName + '</td>'
                    + '<td>' + item.RecruitingCount + '</td>'
                    + '<td>' + item.PostTime + '</td>'
                    + '<td>' + item.GeneralLeader + '</td>'
                    + '<td>' + item.StartingTime + '</td>'
                    + '<td>' + item.StopTime + '</td>'
                    + '<td>' + plantype+ '</td>'
                    + '<td>'
                    + '<a class="layui-btn layui-btn-mini" href="/Personal/Details?id=' + item.Id + '"><i class="iconfont icon-edit"></i> 详情</a>'
                    + '</td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="10">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            $('.news_list thead input[type="checkbox"]').prop("checked", false);
            form.render(); //渲染表单
        }

    }
    //分页
    function showPage(page) {
        laypage({
            cont: 'page',
            pages: page.PageCount,//得到总页数
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }

    $(function () {
        getList(pageData);
    });
})